package com.harmontronics.hanma.paas.paas.service;

import com.harmontronics.hanma.paas.paas.common.MosResponse;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.consts.BaseI18nKeyEnum;
import com.harmontronics.hanma.paas.paas.consts.NumberEnum;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.I18nKeyEnum;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.SsoUserDtoResult;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.SsoUserResult;
import com.harmontronics.hanma.paas.paas.domain.organization.service.MosService;
import com.harmontronics.hanma.paas.paas.domain.tenant.entity.HmTenantUser;
import com.harmontronics.hanma.paas.paas.exception.ErrorKeyException;
import com.harmontronics.hanma.paas.paas.token.MosTokenKit;
import com.harmontronics.hanma.paas.paas.token.UserInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

@Service("hmTenantUserMosService")
@ConditionalOnProperty(prefix = "mos.oss",name="enabled",havingValue = "true")
public class HmTenantUserMosService implements IHmTenantUserService{

    @Autowired
    private MosService mosService;

    @Autowired
    private MosTokenKit mosTokenKit;

    @Override
    public PageBase<HmTenantUser> getList(int pageNo, int pageSize, String tenantUserName) {
        MosResponse<PageBase<SsoUserResult>> response = mosService.searchUser(tenantUserName,pageNo,pageSize);
        if(response.isOk()){
            PageBase<SsoUserResult> pageBase = response.getData();
            List<SsoUserResult> dataList = pageBase.getData();
            List<HmTenantUser> userList = new ArrayList<>();
            for(SsoUserResult userResult : dataList){
                HmTenantUser user = new HmTenantUser();
                user.setId(userResult.getId());
                user.setUserName(userResult.getName());
                user.setIsAdmin(NumberEnum.ZERO.getNum());
                userList.add(user);
            }
            PageBase<HmTenantUser> resultPageBase = PageBase.copy(pageBase, HmTenantUser.class);
            resultPageBase.setData(userList);
            return resultPageBase;
        }
       return null;
    }


    @Override
    public HmTenantUser findById(Long id) {
        MosResponse<SsoUserDtoResult> response = mosService.getUserById(id);
        HmTenantUser tenantUser = new HmTenantUser();
        if(response.isOk()){
            SsoUserDtoResult userResult = response.getData();
            if(Optional.ofNullable(userResult).isPresent()){
                tenantUser.setId(userResult.getId());
                tenantUser.setTenantId(NumberEnum.ONE.getNumLong());
                tenantUser.setUserName(userResult.getName());
                if(userResult.getIsSuperAdmin()){
                    tenantUser.setIsAdmin(NumberEnum.ONE.getNum());
                }else{
                    tenantUser.setIsAdmin(NumberEnum.ZERO.getNum());
                }
                tenantUser.setStatus(userResult.getStatus());
                tenantUser.setDeleted(userResult.getDeleted());
                return tenantUser;
            }

        }
        throw new ErrorKeyException(I18nKeyEnum.SYSTEM_ERROR);
    }

    @Override
    public void logout(){
        mosTokenKit.delete();
    }

    @Override
    public void isAdmin(HmTenantUser hmTenantUser){
        UserInfo userInfo = mosTokenKit.getUserInfo();
        if (!userInfo.isAdmin() && !hmTenantUser.getTenantId().equals(userInfo.getTenantId())) {
            throw new ErrorKeyException(BaseI18nKeyEnum.REQUEST_INVALID);
        }
    }

    @Override
    public void save(HmTenantUser hmTenantUser) {

    }

    @Override
    public void validateField(String key, String value) {

    }

    @Override
    public String login(HmTenantUser hmTenantUser) {
        return null;
    }

    @Override
    public void remove(List<Long> ids) {

    }

    @Override
    public void resetPassword(HmTenantUser hmTenantUser) {

    }

    @Override
    public HmTenantUser updateStatus(Long userId, Integer status) {
        return null;
    }

    @Override
    public UserInfo getUserByToken(){
        return mosTokenKit.getUserInfo();
    }

}
